package com.welsy.user.command
{
	import com.adobe.cairngorm.commands.Command;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import com.welsy.usermgt.business.GetSrcCriteriaDelegate;
	import com.welsy.usermgt.model.UserMgtModelLocator;
	import mx.collections.ArrayCollection;
	import com.adobe.cairngorm.model.ModelLocator;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.iter.vo.IterVO;
	import com.welsy.user.view.iterview.IterView;
	import com.welsy.user.view.iterview.IterView;
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.user.view.iterview.IterView;
	import com.welsy.user.business.GetNewsDelegate;
	import com.welsy.user.event.GetNewsEvent;
	import com.welsy.user.view.homepage.NewsView;
	import com.welsy.qnames.NewsDataTypes;
	import com.welsy.user.vo.NewsVO;
	import com.welsy.user.view.homepage.news.NewView;
	
	public class GetNewsCommand implements Command, IResponder
	{
		
		private var umodel : UserModelLocator = UserModelLocator.getInstance();
		
		public function execute(event : CairngormEvent) : void {
			var delegate : GetNewsDelegate = new GetNewsDelegate(this);
			var eve : GetNewsEvent = GetNewsEvent(event);
			delegate.getNews(eve.userId);
		}
		
		public function result( event : Object ) : void {
			
			var xres : XMLList = XMLList(event.result);
			
			var news : ArrayCollection = new ArrayCollection;
			var nc : XMLList = xres.descendants(NewsDataTypes.TAG_NEWSCOLLECTION).children();
			for each(var nt : XML in nc) {
				var newView : NewView = new NewView;
				var newsVO : NewsVO = new NewsVO;
				newsVO.authorId = nt.child(NewsDataTypes.TAG_AUTHORID);
				newsVO.title = nt.child(NewsDataTypes.TAG_TITLE);
				newsVO.descr = nt.child(NewsDataTypes.TAG_DESCRIPTION);
				newsVO.creationDate = nt.child(NewsDataTypes.TAG_CREATIONDATE);
				newsVO.authorId = nt.child(NewsDataTypes.TAG_AUTHORID);
				newsVO.newsId = nt.child(NewsDataTypes.TAG_NEWSID);
				newView.newsVO = newsVO;
				news.addItem(newView);
			}
			umodel.newsMessages = news;
		}
		
		public function fault(event : Object) : void {
			Alert.show("GetNewsCommand.fault()");
		}
		
	}
}